package main

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	openai "github.com/sashabaranov/go-openai"
	"net/http"
)

func main() {
	c := gin.Default()
	c.GET("/chatgpt", chatgpthandleer)
	c.POST("/postchatgpt", postvalue)
	c.Run()

}
func chatgpthandleer(c *gin.Context) {
	var myselect string
	c.ShouldBind(&myselect)

	//c.ShouldBind(&select)
	//chatgpt("")
	ss := c.Param("myselect")

	fmt.Println(myselect)
	fmt.Println(ss)
	c.String(http.StatusOK, "获取到:%s,%s", myselect, ss)
}
func postvalue(c *gin.Context) {
	s := c.PostForm("myselect")
	d := c.PostForm("text")
	a := ""
	fmt.Println(s)
	if s == "xpdf" {
		a = chatgpt(d)
	} else {
		a = "你可能有问题"
	}

	//c.String(200, "this%s"+string(s))
	c.JSON(200, a)
}
func chatgpt(text string) string {
	//config := openai.DefaultConfig("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")
	//proxyUrl, err := url.Parse("http://localhost:10809")
	//if err != nil {
	//	panic(err)
	//}
	//transport := &http.Transport{
	//	Proxy: http.ProxyURL(proxyUrl),
	//}
	//config.HTTPClient = &http.Client{
	//	Transport: transport,
	//}
	//
	//client := openai.NewClientWithConfig(config)
	client := openai.NewClient("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")
	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model: openai.GPT3Dot5Turbo,
			Messages: []openai.ChatCompletionMessage{
				{
					Role:    openai.ChatMessageRoleUser,
					Content: text,
				},
			},
		},
	)

	if err != nil {
		fmt.Printf("ChatCompletion error: %v\n", err)
		return resp.Choices[0].Message.Content
	}

	fmt.Println(resp.Choices[0].Message.Content)
	return resp.Choices[0].Message.Content
}
